package com.example.demo.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.demo.entity.Pet;
import com.example.demo.entity.PetAndUser;
import com.example.demo.entity.VO.PetVO;
import com.example.demo.result.Result;
import com.example.demo.service.IPetAndUserService;
import com.example.demo.service.IPetService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 宠物表 前端控制器
 * </p>
 *
 * @author Mr.we
 * @since 2024-06-04
 */
@Api(tags = "宠物接口")
@RestController
@RequestMapping("/pet")
public class PetController {

    @Resource
    private IPetService petService;

    @Autowired
    private IPetAndUserService petAndUserService;

    @ApiOperationSupport(author = "Mr.wyy")
    @ApiOperation(value = "根据用户id查询对应的宠物信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",required = true)
    })
    @GetMapping("/findPetByUser")
    public Result<List<Pet>> findPetByUser(@RequestParam Integer userId) {
        List<Pet> petAndUser = petService.findPetByUserId(userId);
        return Result.success(petAndUser).msg("查询成功");
    }


    /**
    * 新增
    * @param
    * @return
    */
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "新增")
    @PostMapping("/add")
    public Result<String> add(@RequestBody PetVO petVO) {
        Pet pet = new Pet();
        BeanUtil.copyProperties(petVO,pet);
        if(!petService.addPet(pet)){
            return Result.error().msg("添加宠物失败");
        }
        //TODO 获取userId添加主人宠物中间表
        int userId = 1;
        PetAndUser petAndUser = new PetAndUser();
        petAndUser.setPetId(pet.getPetId());
        petAndUser.setUserId(userId);
        if(!petAndUserService.insert(petAndUser)){
            return Result.error().msg("宠物主人中间表添加失败");
        }
        return Result.success().msg("添加成功");
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    @ApiOperationSupport(author = "Mr.we")
    @ApiOperation(value = "删除接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "组件id",required = true)
    })
    @DeleteMapping("/delete")
    public Result<String> delete(@RequestParam Integer id) {
        petService.deletePetById(id);
        return Result.success().msg("删除成功");
    }

}